package com.example.mydy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.Task;

import java.util.List;

public class TaskHistoryAdapter extends RecyclerView.Adapter<TaskHistoryAdapter.TaskHistoryViewHolder> {

    private final List<Task> taskList;
    private final Context context;
    private final OnTaskClickListener listener;

    public interface OnTaskClickListener {
        void onTaskClick(Task task);
    }

    public TaskHistoryAdapter(Context context, List<Task> taskList, OnTaskClickListener listener) {
        this.context = context;
        this.taskList = taskList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public TaskHistoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_task_history, parent, false);
        return new TaskHistoryViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull TaskHistoryViewHolder holder, int position) {
        Task task = taskList.get(position);
        holder.bind(task);
    }

    @Override
    public int getItemCount() {
        return taskList.size();
    }
    
    /**
     * 更新任务列表
     * @param newTaskList 新的任务列表
     */
    public void updateTasks(List<Task> newTaskList) {
        this.taskList.clear();
        this.taskList.addAll(newTaskList);
        notifyDataSetChanged();
    }

    public class TaskHistoryViewHolder extends RecyclerView.ViewHolder {
        private final TextView taskTitle;
        private final TextView taskTime;
        private final TextView taskStatus;

        public TaskHistoryViewHolder(@NonNull View itemView) {
            super(itemView);
            taskTitle = itemView.findViewById(R.id.tv_task_title);
            taskTime = itemView.findViewById(R.id.tv_task_time);
            taskStatus = itemView.findViewById(R.id.tv_task_status);

            itemView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && listener != null) {
                    listener.onTaskClick(taskList.get(position));
                }
            });
        }

        public void bind(Task task) {
            taskTitle.setText(task.getTitle());
            taskTime.setText("提交时间：" + task.getSubmittedAt());
            
            // 根据任务状态设置不同的背景和文本
            if (task.isPending()) {
                taskStatus.setText("待审核");
                taskStatus.setBackgroundResource(R.drawable.bg_status_pending);
            } else if (task.isApproved()) {
                taskStatus.setText("已通过");
                taskStatus.setBackgroundResource(R.drawable.bg_status_approved);
            } else if (task.isRejected()) {
                taskStatus.setText("已拒绝");
                taskStatus.setBackgroundResource(R.drawable.bg_status_rejected);
            } else {
                taskStatus.setText("未知状态");
                taskStatus.setBackgroundResource(R.drawable.bg_status_pending);
            }
        }
    }
} 